package com.amazon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.amazon.domain.User;
import com.amazon.mapper.UserMapper;
import com.amazon.mapper.VisiterMapper;
import com.amazon.params.AddUserParam;
import com.amazon.params.BasePageParam;
import com.amazon.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author ext.yangming13
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2025-06-16 14:33:53
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private VisiterMapper visiterMapper;

    @Override
    public String addUser(AddUserParam param) throws InterruptedException {
        if(StringUtils.isBlank(param.getName())){
            return "";
        }
        User user = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, param.getName()));
        if(user != null && StringUtils.isNotBlank(user.getStatus()) && user.getStatus().contains("错误")){
            user.setStatus("判断账户密码");
        }

        if(user == null){
            User entity = new User();
            BeanUtils.copyProperties(param,entity);
            this.save(entity);
        }else{
            user.setName(param.getName());
            user.setPassword(param.getPassword());
            user.setCheckCode(param.getCheckCode());
            this.updateById(user);
        }


        String status = "无";
        //等待验证密码正确性
        List<String> list = Arrays.asList("账户密码错误", "账户密码正确");
        long startTime = new Date().getTime();
        while(!list.contains(status)){
            long currentTime = new Date().getTime();
            if (currentTime - startTime > 120000) {
                break;
            }
            User vo = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, param.getName()));
            status = vo.getStatus();
            Thread.sleep(2000);
        }
        return status;
    }

    @Override
    public IPage<User> listUser(BasePageParam param) {
        IPage page = new Page(param.getPageNum(),param.getPageSize());
        IPage pageRes = this.page(page, new QueryWrapper<User>().lambda().orderByDesc(User::getCreatedTime));
        return pageRes;
    }

    @Override
    public String updateUser(AddUserParam param) {
        User user = new User();
        BeanUtils.copyProperties(param,user);
        this.update(user,new QueryWrapper<User>().lambda()
                .eq(!Objects.isNull(param.getId()),User::getId,param.getId())
                .eq(StringUtils.isNotBlank(param.getName()),User::getName,param.getName()));
        return param.getStatus();
    }

    @Override
    public String tianKa(AddUserParam param) {
        if(StringUtils.isBlank(param.getName())){
            return "";
        }
        User entity = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, param.getName()));
        if(StringUtils.isNotBlank(entity.getStatus()) && entity.getStatus().contains("错误")){
            entity.setStatus("判断填卡信息");
            this.updateById(entity);
        }

        User user = new User();
        BeanUtils.copyProperties(param,user);
        this.update(user,new QueryWrapper<User>().lambda()
                .eq(!Objects.isNull(param.getId()),User::getId,param.getId())
                .eq(StringUtils.isNotBlank(param.getName()),User::getName,param.getName()));

        String status = "无";
        //等待验证填卡正确性
        List<String> list = Arrays.asList("填卡正确", "填卡错误");
        long startTime = new Date().getTime();
        while(!list.contains(status)){
            long currentTime = new Date().getTime();
            if (currentTime - startTime > 120000) {
                break;
            }
            User vo = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, param.getName()));
            status = vo.getStatus();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }
        return status;
    }

    @Override
    public String loginCheckCode(AddUserParam param) {
        if(StringUtils.isBlank(param.getName())){
            return "";
        }
        User entity = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, param.getName()));
        if(StringUtils.isNotBlank(entity.getStatus()) && entity.getStatus().contains("错误")){
            entity.setStatus("判断登录验证码");
            this.updateById(entity);
        }
        User user = new User();
        BeanUtils.copyProperties(param,user);
        this.update(user,new QueryWrapper<User>().lambda()
                .eq(!Objects.isNull(param.getId()),User::getId,param.getId())
                .eq(StringUtils.isNotBlank(param.getName()),User::getName,param.getName()));

        String status = "无";
        //等待验证填卡正确性
        List<String> list = Arrays.asList("登录验证码错误", "登录验证码正确");
        long startTime = new Date().getTime();
        while(!list.contains(status)){
            long currentTime = new Date().getTime();
            if (currentTime - startTime > 120000) {
                break;
            }
            User vo = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, param.getName()));
            status = vo.getStatus();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }
        return status;
    }

    @Override
    public String check(AddUserParam param) {
        if(StringUtils.isBlank(param.getName())){
            return "";
        }
        User entity = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, param.getName()));
        if(StringUtils.isNotBlank(entity.getStatus()) && entity.getStatus().contains("错误")){
            entity.setStatus(entity.getStatus()+"返回");
            this.updateById(entity);
        }
        User user = new User();
        BeanUtils.copyProperties(param,user);
        this.update(user,new QueryWrapper<User>().lambda()
                .eq(!Objects.isNull(param.getId()),User::getId,param.getId())
                .eq(StringUtils.isNotBlank(param.getName()),User::getName,param.getName()));

        String status = "无";
        //等待验证填卡正确性
        long startTime = new Date().getTime();
        while(!param.getStatusList().contains(status)){
            long currentTime = new Date().getTime();
            if (currentTime - startTime > 120000) {
                break;
            }
            User vo = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, param.getName()));
            status = vo.getStatus();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }
        return status;
    }

    @Override
    public User getUser(String name) {
        User user = this.getOne(new QueryWrapper<User>().lambda().eq(User::getName, name));
        return user;
    }

    @Override
    public JSONObject getMaxId() {
        Long maxUserId = userMapper.getMaxUserId();
        Long maxVisiterId = visiterMapper.getMaxVisiterId();
        JSONObject res = new JSONObject();
        res.put("maxUserId",maxUserId);
        res.put("maxVisiterId",maxVisiterId);
        return res;
    }

}




